﻿@page "/Admin/Poss"
@attribute [Authorize]
@attribute [TabItemOption(Text ="职位管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysPosService SysPosService

<div class="overflow-hidden h-100">
    <Table TItem="SysPos"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="false"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_search" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="250"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnSaveAsync="@OnSaveAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" />
            <TableColumn @bind-Field="@context.Code" />
            <TableColumn @bind-Field="@context.Sort" />
            <TableColumn @bind-Field="@context.Remark" />
        </TableColumns>
        <EditTemplate>
            <div class="row g-3 form-inline">
                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                </div>

                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                </div>

                <div class="col-12 col-sm-6 col-md-12">
                    <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" Step="5" />
                </div>

                <div class="col-12 col-sm-6 col-md-12">
                    <Textarea @bind-Value="@context.Remark" maxlength="50" />
                </div>
            </div>
        </EditTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                    </div>
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.Code" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private Table<SysPos>? _table;
    private SysPos _search = new();

    private Task<QueryData<SysPos>> OnQueryAsync(QueryPageOptions options)
        => SysPosService.PageAsync(_search.Adapt<PosPageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<SysPos> items) 
        => SysPosService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(SysPos item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysPosService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysPosService.EditAsync(item);
                break;
        }
        return result;
    }
}